package com.blast.cn

import android.content.Intent
import android.content.IntentFilter
import android.view.KeyEvent
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import com.blast.apply.ui.ApplyListActivity
import com.blast.cn.ui.BlastLogActivity
import com.blast.cn.ui.SettingsActivity
import com.blast.cn.ui.VoltageSettingsActivity
import com.blast.cn.work.UploadBlastWork
import com.blast.manager.ui.ManagerMainActivity
import com.wxl.common.AppContext
import com.wxl.common.base.AbsActivity
import com.wxl.common.event.LookBlastLogEvent
import com.wxl.common.event.VoltageSettingsEvent
import com.wxl.common.life.ViewModelQuick
import com.wxl.common.presenter.UploadCollectorPresenter
import com.wxl.common.receiver.BatteryReceiver
import com.wxl.common.serialport.CJQUpdateModel
import kotlinx.android.synthetic.main.activity_app_main_layout.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class AppMainActivity : AbsActivity() {

    private var mBatteryReceiver: BatteryReceiver? = null

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_app_main_layout
    }

    override fun onCreateViewChanged() {

        EventBus.getDefault().register(this)

        setTopTitle("起爆器")

        CJQUpdateModel.checkNetCjqVersion()

        mainSubText.text = "控制器版本:${AppContext.appContext.getDeviceVersion()}"

        mainApplyBtn.setOnClickListener {
            startActivity(Intent(this, ApplyListActivity::class.java))
        }

        mainManagerBtn.setOnClickListener {
            startActivity(Intent(this, ManagerMainActivity::class.java))
        }

        mainRecordBtn.setOnClickListener {
            val e = LookBlastLogEvent()
            onLookBlastLogEvent(e)
        }

        mainSystemBtn.setOnClickListener {
            startActivity(Intent(this, SettingsActivity::class.java))
        }

        startUploadBlastRecordWork()

        registerReceiver()
    }


    /**
     * 开始上传起爆记录任务
     */
    private fun startUploadBlastRecordWork() {
        WorkManager.getInstance(AppContext.appContext.getApplication())
            .beginUniqueWork(
                "uploadBlastRecord",
                ExistingWorkPolicy.REPLACE,
                OneTimeWorkRequest.Builder(UploadBlastWork::class.java).build()
            ).enqueue()
    }

    override fun onResume() {
        super.onResume()
        ViewModelQuick.get(this, UploadCollectorPresenter::class.java).onCreated()
    }


    private fun registerReceiver() {
        // 注册电量监听广播
        val filter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        mBatteryReceiver = BatteryReceiver()
        registerReceiver(mBatteryReceiver, filter)
    }

    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_F1) {
            mainApplyBtn.performClick()
        }
        if (keyCode == KeyEvent.KEYCODE_F2) {
            mainManagerBtn.performClick()
        }
        if (keyCode == KeyEvent.KEYCODE_F3) {
            mainRecordBtn.performClick()
        }
        if (keyCode == KeyEvent.KEYCODE_F4) {
            mainSystemBtn.performClick()
        }
        return false
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        unregisterReceiver(mBatteryReceiver)
    }


    /**
     * 查看爆破日志
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLookBlastLogEvent(e: LookBlastLogEvent) {
        val intent = Intent(this, BlastLogActivity::class.java)
        //intent.putExtra("data", e.blast as ArrayList)
        startActivity(intent)
    }

    /**
     * 进入电压设置
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onVoltageSettingsEvent(e: VoltageSettingsEvent){
        startActivity(Intent(this, VoltageSettingsActivity::class.java))
    }
}